﻿/// Deep Earth is a community project available under the Microsoft Public License (Ms-PL)
/// Code is provided as is and with no warrenty – Use at your own risk
/// View the project and the latest code at http://codeplex.com/deepearth/

using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using DeepEarth.Layers;
using System.Runtime.InteropServices;

namespace DeepEarth.Provider.GoogleMap
{
    public enum GmMapModes
    {
        GoogleMapWeb,
        GoogleMapLocal,
        GoogleMapWebAndLocal
    }

    public class GmTileSource : TileSource 
    {
        private const string TilePathGoogleMapWeb = @"http://mt3.google.cn/vt/lyrs=m@133&hl=zh-CN&gl=cn&x={1}&y={2}&z={0}&s=Galileo";
        private readonly string TilePathGoogleMapLocal = string.Empty;
        private bool _IsInitialized;

        private bool _IsTileDownloadStarted;
        private GmMapModes _MapMode;

        //Constructor Called by XAML instanciation; Wait for MapMode to be set to initialize services
        public GmTileSource(): base()
        {
            // Create Url for local tiles 
            string path = HtmlPage.Document.DocumentUri.AbsolutePath;
            path = path.Substring(0, path.LastIndexOf("/")) + "/ClientBin/MapCache/{0}/x{1}y{2}.png";
            TilePathGoogleMapLocal = string.Concat("http://", HtmlPage.Document.DocumentUri.Host, ":", HtmlPage.Document.DocumentUri.Port, path);
        }

        public GmTileSource(GmMapModes mode) : this()
        {
            MapMode = mode;
        }

        public GmMapModes MapMode
        {
            get { return _MapMode; }
            set
            {
                if (_IsTileDownloadStarted)
                {
                    throw new InvalidOperationException();
                }

                _MapMode = value;
                ID = value.ToString();
                _IsInitialized = true;
                if (InitializeCompleted != null) InitializeCompleted(this, null);
            }
        }

        public override bool IsInitialized
        {
            get { return _IsInitialized; }
        }

        public override Color TileColor
        {
            get
            {
                Color baseColor = Colors.Black;
                switch (_MapMode)
                {
                    case GmMapModes.GoogleMapWeb:
                        baseColor = Color.FromArgb(0xFF, 0x1A, 0x44, 0x7A);
                        break;
                    case GmMapModes.GoogleMapLocal:
                        baseColor = Color.FromArgb(0xFF, 0x1F, 0x44, 0x7A);
                        break;
                    case GmMapModes.GoogleMapWebAndLocal:
                        baseColor = Color.FromArgb(0xFF, 0x1E, 0x44, 0x7A);
                        break;
                }
                return baseColor;
            }
        }


        public override UIElement GetCopyright()
        {
            //if (MapMode == GmMapModes.GoogleMapWeb)
            //{
            //    const string logoPath = @"http://media.amazonwebservices.com/Powered-by-Amazon-Web-Services.jpg";
            //    return new Image { Source = new BitmapImage(new Uri(logoPath)), Stretch = Stretch.None };
            //}
            return null;
        }


        public override Uri GetTile(int tileLevel, int tilePositionX, int tilePositionY)
        {
            if (IsInitialized)
            {
                int zoom = TileToZoom(tileLevel);
                _IsTileDownloadStarted = true;

                string url = string.Empty;

                switch (MapMode)
                {
                    case GmMapModes.GoogleMapWebAndLocal:
                    case GmMapModes.GoogleMapLocal:
                        url = TilePathGoogleMapLocal;
                        int port = Application.Current.Host.Source.Port;
                        url = string.Format(url, zoom, tilePositionX, tilePositionY, port);
                        break;
                    case GmMapModes.GoogleMapWeb:
                        url = TilePathGoogleMapWeb;
                        url = string.Format(url, zoom, tilePositionX, tilePositionY);
                        if (InitializeCompleted != null)
                        {
                            SenderGmArgs sa = new SenderGmArgs()
                            {
                                Url = url,
                                Zoom = zoom.ToString(),
                                TilePositionX = tilePositionX.ToString(),
                                TilePositionY = tilePositionY.ToString()
                            };

                            InitializeCompleted(sa, null);

                        }
                        break;
                    default:
                        
                        break;
                   
                }
                return new Uri(url);
            }
            return null;
        }

        public override event EventHandler InitializeCompleted;
        
    }

    public class SenderGmArgs
    {
        public string Url;
        public string Zoom;
        public string TilePositionX;
        public string TilePositionY;
    }
}